package com.rongshu.view.login.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.RxHelper;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.widget.PhoneEditText;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import rx.Subscriber;

/**
 * 忘记密码
 */
public class ForgetPasswordActivity extends BaseActivity {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;//标题
    @BindView(R.id.edtPhone)
    PhoneEditText edtPhone;
    @BindView(R.id.txtNextStep)
    TextView txtNextStep;

    private int currentTimes = 60;
    //request
    private final int REQUEST_CODE = 0x01;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        immersionBar.keyboardEnable(true, WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
                | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        immersionBar.init();
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_forget_password_view;
    }

    @Override
    protected void initView() {
        //设置title
        txt_top_title.setText(R.string.top_forget_password);
        txtNextStep.setClickable(false);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @OnTextChanged(value = R.id.edtPhone, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void phoneTextChanged(Editable editable) {
        txtNextStep.setBackgroundResource(editable.toString().length() == 13 ? R.drawable.shape_btn_3cc051 :
                R.drawable.shape_btn_8fe49d);
        txtNextStep.setClickable(editable.toString().length() == 13);
        if (editable.toString().length() == 13) {
            showKeyboard(false);
        }
    }

    @OnClick({R.id.layout_back, R.id.txtNextStep})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                showKeyboard(false);
                finish();
                break;
            case R.id.txtNextStep:
                showKeyboard(false);
                String phone = edtPhone.getPhoneText();
                Intent intent = new Intent(ForgetPasswordActivity.this, InputAuthCodeActivity.class);
                intent.putExtra(Constant.INTENT_DATA, phone);
                intent.putExtra(Constant.INTENT_TYPE, Constant.FIND_PWD);
                intent.putExtra(Constant.INTENT_MESSAGE, currentTimes);
                startActivityForResult(intent, REQUEST_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE) {
                if (data != null && data.hasExtra(Constant.INTENT_MESSAGE)) {
                    currentTimes = data.getIntExtra(Constant.INTENT_MESSAGE, 0);
                    cutDownTime();
                } else {
                    setResult(RESULT_OK);
                    finish();
                }
            }
        }
    }

    //倒计时操作
    private void cutDownTime() {
        RxHelper.countdown(currentTimes)
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        currentTimes = integer;
                    }
                });
    }
}
